(ns liberator-sample.core.documentation.resource-definition
  (:require [liberator.core :refer [defresource resource]]
            [liberator.dev :refer [wrap-trace]]
            [compojure.core :refer [defroutes ANY]]
            [ring.middleware.params :refer [wrap-params]]
            [org.httpkit.server :as server]
            [clojure.java.io :as io]
            [clojure.data.json :as json])
  (:use clojure.pprint))

;; resource and defresource
(declare foo)
(defresource foo
             :handle-ok "This is ok")

(def foo (resource :handle-ok "This is ok"))
(def foo1 (resource :handle-ok (fn [context] "We can use a function.")))
(def foo2 (resource :handle-ok "We can also use a plain value"))


;; Base implementation
(def resource-defaults
  {:handle-not-found (fn [_] "Ops.")
   :authorized?      (fn [_] true)})

(defresource foo
             resource-defaults
             :handle-ok "This is ok")
;; this is the same as
(defresource foo
             :handle-ok "This is ok"
             :handle-not-found (fn [_] "Ops.")
             :authorized? (fn [_] true))

;; Parametrized resources
(declare parametrized x params)
(defresource parametrized [x]
             :handle-ok (fn [_] (format "This is x: %s" x)))

(defroutes params
           ;; these are equivalent:
           (ANY "/a/:x" [x]
                (resource :handle-ok (fn [_] (format "This is x: %s" x))))
           (ANY "/b/:x" [x]
                (parametrized x)))